# _*_ coding utf-8 _*_
# FILENAME：.py
# DESCRIPTION: 
# AUTH: 
# DATE: 2022/6/110:56 上午


from components.BaseUploadDialog import BaseUploadDialog, BaseUpload
from constant import UploadType, UploadValidateType
from models.baseModel import get_session
from models.partsModel import PartsDetailsModel
from utils.confUtils import read_performance_config


class UploadDialog(BaseUploadDialog):
    @staticmethod
    def get_dialog(parent=None, id=0):
        dialog = UploadDialog(parent, UploadType.PARTS.value, id, Upload)
        return dialog.exec()


"""
上传类
"""


class Upload(BaseUpload):
    def save(self, sheet):
        rows = sheet.nrows  # 行
        cols = sheet.ncols  # 列

        session = get_session()

        try:
            # 分批保存数量
            bulk_save_count = read_performance_config('bulk_save_count')

            # 用于批量提交
            details_list = []

            # 删除从表数据
            session.query(PartsDetailsModel).filter(PartsDetailsModel.parts_id == self._id).delete()

            for i in range(0, rows):
                # 从表model
                details = PartsDetailsModel()
                details.parts_id = self._id

                for c in range(0, cols):
                    value = sheet.row(i)[c].value
                    if i == 0:
                        # 检验标题是否正确
                        if (c == 0 and value != '左右件') or (c == 1 and value != '装车朝向') or (
                                c == 2 and value != '零件件号') or (c == 3 and value != '工厂') \
                                or (c == 4 and value != '卸货道口') or (c == 5 and value != '存储区安全库存') or (
                                c == 6 and value != '存储区最大库存') \
                                or (c == 7 and value != '零件名称') or (c == 8 and value != '供应商名称') or (
                                c == 9 and value != '包装类型') \
                                or (c == 10 and value != '料架是否能和托盘堆') or (c == 11 and value != '长') or (
                                c == 12 and value != '宽') \
                                or (c == 13 and value != '高') or (c == 14 and value != 'SNP'):
                            raise Exception('上传文件模版错误！')
                    else:
                        if c == 0:
                            details.lr_parts = value
                        elif c == 1:
                            details.loading_direction = value
                        elif c == 2:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value], '零件件号', i + 1)
                            details.code = value
                        elif c == 3:
                            details.factory = value
                        elif c == 4:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value, UploadValidateType.IS_NUMBER.value], '卸货道口', i + 1)
                            details.unloading_crossing = value
                        elif c == 5:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value, UploadValidateType.IS_NUMBER.value], '存储区安全库存', i + 1)
                            details.safety_stock = value
                        elif c == 6:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value, UploadValidateType.IS_NUMBER.value], '存储区最大库存', i + 1)
                            details.max_stock = value
                        elif c == 7:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value], '零件名称', i + 1)
                            details.name = value
                        elif c == 8:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value], '供应商名称', i + 1)
                            details.supplier_name = value
                        elif c == 9:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value], '包装类型', i + 1)
                            details.packaging_type = value
                        elif c == 10:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value], '料架是否能和托盘堆', i + 1)
                            details.material_rack_pallet_adapted = value
                        elif c == 11:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value, UploadValidateType.IS_NUMBER.value], '长', i + 1)
                            details.length = value
                        elif c == 12:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value, UploadValidateType.IS_NUMBER.value], '宽', i + 1)
                            details.width = value
                        elif c == 13:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value, UploadValidateType.IS_NUMBER.value], '高', i + 1)
                            details.height = value
                        elif c == 14:
                            self.validate(value, [UploadValidateType.NOT_EMPTY.value, UploadValidateType.IS_NUMBER.value], 'SNP', i + 1)
                            details.snp = value
                        else:
                            continue

                # 保存行
                if i > 0:
                    details_list.append(details)
                    # 分批保存
                    if 0 < int(bulk_save_count) <= len(details_list):
                        # 保存
                        session.bulk_save_objects(details_list)
                        details_list = []

            # 保存
            session.bulk_save_objects(details_list)
            # 提交
            session.commit()
            session.close()
        except Exception as e:
            raise Exception(e)
        finally:
            session.close()
